var app = getApp();
var url = app.url;
Page({
  data: {
    listData: [],
    scale: '11',
    latitude: '',
    longitude: '',
    markers: [],
    canLook: false,
    id:"",
    isWorker:false,
    distance:0,
  },
  onLoad: function(options) {
    console.log(options)
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: (res) => {
        console.log(res)
        this.setData({
          scale: 20,
          longitude: res.longitude,
          latitude: res.latitude,
          // markers: that.getLightMarkers(),
        })
      }
    });
    var userName = wx.getStorageSync("userName")
    var id = options.id;
      this.setData({
        id: options.id,
      });
     this.getDetail()
    var interval;
    if(options.persion){
      this.setData({isWorker:true})
      interval = setInterval( ()=> {
        this.sendArea();
      },10000)
    }else{
      
      interval = setInterval( ()=> {
        this.getDetail()
      },10000)
    }
   
   this.setData({interval})
    
  },
  onReady: function() {
    // 页面渲染完成
    this.mapCtx = wx.createMapContext("map");

  },
  onShow: function() {
    // 页面显示

  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
    console.log("onUnload")
    clearInterval(this.data.interval)
  },
  onShareAppMessage: function() {

  },
  getDetail(){
    var that = this;
    var params = { reportId: this.data.id };
    wx.request({
      url: url + '/web/exceptionReport/getExceptionReportById.htm',
      data: app.ecbEncrypto(params),
      method: 'POST',
      success:  (res)=> {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr);
        console.log(rest)
        if (rest.isSuccess == 0) {
          var info = rest.reportInfo;
          var userInfo = rest.userInfo;
          info.createTime = new Date(info.createTime).Format("yyyy-MM-dd hh:mm:ss");
          that.setData({ info: info,userInfo:userInfo,distance:(Number(rest.distance)/1000).toFixed(2) })
          this.createMarker() 
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
      },
    })
  },
  callPhone(){
    wx.makePhoneCall({
      phoneNumber: this.data.userInfo.phone,
    })
  },
  createMarker() {
    let point = this.data.info;
    let arr = point.lngLat.split(",");
    let latitude = arr[1];
    let longitude = arr[0];
    let marker = {
      iconPath: "../../images/light_02.png",
      id: point.id || 0,
      state: point.state,
      name: '路灯',
      title: point.address,
      latitude: latitude,
      longitude: longitude,
      exceptionReportId: point.exceptionReportId,
      zIndex: 1,
      label: {
        x: -24,
        y: -26,
        content: ''
      },
      width: 25,
      height: 25
    };
    let userInfo = this.data.userInfo
   
    console.log(this.data.isWorker)
    if(!this.data.isWorker && userInfo){
      let personMarker = {
        iconPath: "../../images/person.png",
        name: '维修员',
        latitude: userInfo.lat,
        longitude: userInfo.lng,
        zIndex: 1,
        label: {
          x: -24,
          y: -26,
          content: ''
        },
        width: 40,
        height: 40
      };
      this.setData({
        markers: [marker,personMarker],
      })  
    }else{
      this.setData({
        markers: [marker],
      })
    }
  },
  hasGo(){
    wx.request({
      url: `https://www.xingyuwulian.com/api/local/changeExceptionReportState?userId=${wx.getStorageSync('userId')}&id=${this.data.id}&orderState=2`,
      data: {},
      method: "post",
      success:  (res)=> {
        var res = res.data;
        console.log(res);
        if(res.code == 1){
          wx.showToast({
            title: '已出发',
            duration: 1000,
          })
          this.getDetail()
        }
      }
    })
  },
  hasArrive(){
    wx.request({
      url: `https://www.xingyuwulian.com/api/local/changeExceptionReportState?userId=${wx.getStorageSync('userId')}&id=${this.data.id}&orderState=3`,
      data: {},
      method: "post",
      success:  (res)=> {
        var res = res.data;
        console.log(res);
        if(res.code == 1){
          wx.showToast({
            title: '已到达',
            duration: 1000,
          })
          this.getDetail()
        }
      }
    })
  },
  sendArea(){
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: (res) => {
        console.log(res)
        wx.request({
          url: `https://www.xingyuwulian.com/api/local/setUserLocal?userId=${wx.getStorageSync('userId')}&lat=${res.latitude}&lng=${res.longitude}`,
          data: {},
          method: "post",
          success:  (res)=> {
            var res = res.data;
            // console.log(res);
          }
        })
      }
    });
  },
  toStart(){
    wx.request({
      url: `https://www.xingyuwulian.com/api/exceptionReport/dealWith?id=${this.data.id}&userId=${wx.getStorageSync('userId')}`,
      data: {},
      method: "post",
      success:  (res)=> {
        var res = res.data;
        console.log(res);
        if(res.code == 1){
          wx.showToast({
            title: '已受理',
            duration: 1000,
          })
          this.getDetail()
        }
      }
    })
  },
});